﻿using System;
using System.Collections.Generic;
using System.Linq;
using Contracts.Entities;

namespace Model.MovieElements
{
    public class SynchronizeMovieElements
    {
        public void Process(IEnumerable<MovieElement> dbItems, IEnumerable<MovieElement> myItems)
        {
            var results = new List<MovieElement>();

            if (dbItems == null || myItems == null)
                return;

            foreach (var dbItem in dbItems)
            {
                var myItem = myItems.SingleOrDefault(m => m.Name == dbItem.Name);

                if (myItem == null)
                    dbItem.SyncState = SyncState.Deleted;

                results.Add(dbItem);
            }

            foreach (var myItem in myItems)
            {
                if (dbItems.SingleOrDefault(m => m.Name == myItem.Name) == null)
                {
                    myItem.SyncState = SyncState.Inserted;
                    results.Add(myItem);
                }
            }
           
            OnCompleted(results);
        }

        public event Action<IEnumerable<MovieElement>> OnCompleted;
    }
}
